package algorithm.exam.queue;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class maxSlidingWindow {

    public int[] maxSlidingWindow(int[] nums, int k) {
        int[] res =new int[nums.length-k+1];
        int j=0;
        PriorityQueue<int[]> queue =new PriorityQueue<>((x1,x2)->x2[0]-x1[0]);
        for(int i=0;i<nums.length;i++){
            int[] array ={nums[i],i};
            queue.add(array);
            if(i>=k){
               while (queue.peek()[1]<=i-k){
                   queue.poll();
               }

            }
            if(i>=k-1){
                res[j]=queue.peek()[0];
                j++;
            }
        }
    return res;
    }




    public static void main(String[] args) {
        int[] nums ={9,10,9,-7,-4,-8,2,-6};
        maxSlidingWindow mm =new maxSlidingWindow();
        int[] res =  mm.maxSlidingWindow(nums,5);
        for (int re : res) {
            System.out.println(re);
        }
    }
}
